﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Capa_Entidad;

namespace Acceso_Datos.Clases_DAO
{
    public class clsAte_Atencion_ExamenDAO
    {

        private BDClinicaEntities ContextoDeObjs = new BDClinicaEntities();

        public bool Insertar(ATE_ATENCION_EXAMEN Obj)
        {
            try
            {
                ContextoDeObjs.AddObject("ATE_ATENCION_EXAMEN", Obj);
                ContextoDeObjs.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Modificar(ATE_ATENCION_EXAMEN obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("ATE_ATENCION_EXAMEN", obj);
                ContextoDeObjs.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
                ContextoDeObjs.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }
        }

        public bool Eliminar(ATE_ATENCION_EXAMEN obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("ATE_ATENCION_EXAMEN", obj);
                ContextoDeObjs.DeleteObject(obj);
                ContextoDeObjs.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public IEnumerable<Object> listar(int? idAtencion)
        {
            IEnumerable<Object> lista = new List<Object>();

            try
            {
                var q = from c in ContextoDeObjs.ATE_ATENCION_EXAMEN
                        where c.ATE_ID_ATENCION == idAtencion
                        orderby c.ATE_DESCRIPCION
                        select c;

                lista = q.ToList();
            }
            catch (Exception)
            {
                lista = null;
                throw;
            }

            return lista;
        }

        public int? ObtenerUltimoID()
        {
            int? idAtencion = 1;
            try
            {
                var q = from t in
                            (from t in ContextoDeObjs.ATE_ATENCION_EXAMEN
                             select new
                             {
                                 t.ATE_ID_EXAMEN,
                                 Dummy = "x"
                             })
                        group t by new { t.Dummy } into g
                        select new
                        {
                            ATE_ID_EXAMEN = (System.Int32?)(g.Max(p => p.ATE_ID_EXAMEN) + 1)
                        };
                if (Convert.ToInt16(q.Count()) <= 0)
                {
                    idAtencion = 1;
                }
                else {
                    idAtencion = q.ToList()[0].ATE_ID_EXAMEN;
                }
                
            }
            catch (Exception)
            {
                idAtencion = 1;
                throw;
            }

            return idAtencion;
        }
    }
}
